package com.logic.modular.game.signIn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.exception.ServiceException;
import com.logic.comment.util.RedisUtils;
import com.logic.modular.game.player.bag.service.BagItemServiceImpl;
import com.logic.modular.game.statistical.service.impl.PlayerStatisticalPoolServiceImpl;
import com.logic.modular.game.signIn.domain.PlayerSignInAward;
import com.logic.modular.game.signIn.service.PlayerSignInAwardService;
import com.logic.modular.gameCore.cultivate.signIn.domain.CoreSignInAward;
import com.logic.modular.gameCore.cultivate.signIn.service.impl.CoreSignInAwardServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 用户签到服务实现类
 */
@Service
public class PlayerSignInAwardServiceImpl implements PlayerSignInAwardService {
    @Resource
    private PlayerStatisticalPoolServiceImpl playerStatisticalPoolService;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private BagItemServiceImpl bagItemServiceImpl;
    @Resource
    private CoreSignInAwardServiceImpl coreSignInAwardService;

    public List<PlayerSignInAward> list(Long playerId, int i) {
        return coreSignInAwardService.list(new LambdaQueryWrapper<CoreSignInAward>().eq(CoreSignInAward::getGrouping, i))
                .stream().map(item -> {
                    Long id = item.getId();
                    String format = String.format("%s:%d:%d", "signIn7thClaimRewards", playerId, id);
                    Boolean b = redisUtils.hasKey(format);
                    PlayerSignInAward playerSignInAward = new PlayerSignInAward();
                    playerSignInAward.setUid(playerId);
                    playerSignInAward.setCoreSignInAward(item);
                    playerSignInAward.setStatus(0);
                    if (b) {
                        playerSignInAward.setStatus(1);
                    }
                    return playerSignInAward;
                }).collect(Collectors.toList());
    }

    @Transactional
    public boolean signIn7thClaimRewards(Long playerId, Long id) {
        String format = String.format("%s:%d:%d", "signIn7thClaimRewards", playerId, id);
        Boolean b = redisUtils.hasKey(format);
        if (b) {
            throw new ServiceException("已领取");
        }

        Long l = playerStatisticalPoolService.obtain(playerId).conditionValue2.get(60);
        CoreSignInAward cache = CoreSignInAwardServiceImpl.getCache(id);
        if (cache.getDay() <= l) {
            cache.getAward1().forEach((k, v) -> bagItemServiceImpl.obtainBagItem(playerId, k, BigDecimal.valueOf(v)));
            redisUtils.set(format, 1);
        }

        return true;
    }

    @Transactional
    public boolean signIn30thClaimRewards(Long playerId, Long id) {
        String format = String.format("%s:%d:%d", "signIn30thClaimRewards", playerId, id);
        Boolean b = redisUtils.hasKey(format);
        if (b) {
            throw new ServiceException("已领取");
        }

        CoreSignInAward cache = CoreSignInAwardServiceImpl.getCache(id);
        if (cache.getType() == 1) {
            Long l = playerStatisticalPoolService.obtain(playerId).conditionValue2.get(60);
            if (cache.getDay() <= l) {
                cache.getAward1().forEach((k, v) -> bagItemServiceImpl.obtainBagItem(playerId, k, BigDecimal.valueOf(v)));
                redisUtils.set(format, 1, 30, TimeUnit.DAYS);
            }
        }

        return true;
    }
}
